<?php
/**
 * Created by PhpStorm.
 * Script Name: Tpzs.php
 * Create: 2022/4/18 10:38
 * Description: 优惠券助手处理器
 * Author: fudaoji<fdj@kuryun.cn>
 */
namespace app\bot\controller;

use app\common\model\yhq\Code;
use app\common\model\yhq\Config;
use app\constants\Reply;
use ky\Logger;

class Yhq extends Addon
{
    private $switch;
    private $toWxid = '';
    /**
     * @var Config
     */
    private $configM;
    /**
     * @var \app\common\model\yhq\Reply
     */
    private $replyM;
    /**
     * @var Code
     */
    private $codeM;

    public function init($options = [])
    {
        parent::init($options); // TODO: Change the autogenerated stub
        $this->configM = new Config();
        $this->replyM = new \app\common\model\yhq\Reply();
        $this->codeM = new Code();
        $this->switch = $this->configM->getOneByMap([
            'bot_id' => $this->bot['id'], 'key' => 'switch', 'value' => 1
        ]);
        return $this;
    }

    public function groupMemberAddHandle(){
        if(empty($this->switch)){
            return false;
        }
        $guest = $this->botClient->getGuest($this->content);
        $nickname = $guest['nickname'];

        //回复消息
        $replys = $this->replyM->getAll([
            'where' => [
                'bot_id' => $this->bot['id'],
                'event' => Reply::FRIEND_IN,
                'status' => 1
            ]
        ]);
        foreach ($replys as $k => $reply){
            if(empty($reply['wxids']) || strpos($reply['wxids'], $this->groupWxid) !== false){
                $code = $this->codeM->getCode(['coupon_id' => $reply['coupon_id']]);
                $msg = str_replace(['[昵称]', '[优惠券码]', '[优惠券链接]'], [$nickname, $code['code'], $code['code_url']], $reply['content']);
                $this->botClient->sendTextToFriends([
                    'robot_wxid' => $this->botWxid,
                    'to_wxid' => $this->groupWxid,
                    'msg' => $msg
                ]);
                $this->codeM->offCode(['code_id' => $code['id'], 'wxid' => $guest['wxid']]);
            }
        }
    }

    /**
     * 机器人主动事件
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function deviceCallbackHandle(){
        if(!empty($this->groupWxid)){
            $this->groupChatHandle();
        }
    }

    /**
     * 群聊处理器
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function groupChatHandle(){
        if(empty($this->switch)){
            return false;
        }
        $this->toWxid = $this->groupWxid;
        //一、关键词
        $this->keyword();
    }

    /**
     * 私聊处理器
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function privateChatHandle(){
        if(empty($this->switch)){
            return false;
        }
        $this->toWxid = $this->fromWxid;
        //一、关键词
        $this->keyword();
    }

    /**
     * 关键词回复
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    private function keyword(){
        if($this->content['msg'] == 'good'){
            $this->botClient->sendTextToFriends([
                'robot_wxid' => $this->botWxid,
                'to_wxid' => $this->groupWxid,
                'msg' => 'ooooo'
            ]);
        }
    }
}